package cn.jd.org.controller;

import cn.jd.basic.anno.PreAuthorize;
import cn.jd.basic.config.BussinessException;
import cn.jd.basic.util.ExcelUtil;
import cn.jd.org.domain.ShopAuditLog;
import cn.jd.org.domain.Systemdictionarydetail;
import cn.jd.org.dto.ShopDto;
import cn.jd.org.service.IShopService;
import cn.jd.org.domain.Shop;
import cn.jd.org.query.ShopQuery;
import cn.jd.basic.util.AjaxResult;
import cn.jd.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.Cache;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
@PreAuthorize
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "店铺的添加和修改",sn = "shop:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "根据id删除店铺",sn = "shop:remove")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据id查询店铺",sn = "shop:findById")
    public Shop findById(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "查询所有店铺",sn = "shop:findAll")
    public List<Shop> findAll(){

        return shopService.findAll();
    }



    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "店铺的高级查询和分页查询",sn = "shop:queryByPageOrLog")
    public PageList<Shop> queryByPageOrLog(@RequestBody ShopQuery query) {
        PageList<Shop> shop=shopService.queryByPageOrLog(query);
       /* PageList<Shop> shop=shopService.queryByPage(query);*/
        for (Shop row : shop.getRows()) {
            row.setLogo("http://123.207.27.208"+row.getLogo());
        }
        return shop;
    }





    /** 商家入住信息登录接口 settlement*/
    @PostMapping("/settlement")

    public AjaxResult settlement(@RequestBody Shop shop){
       try{
           shopService.settlement(shop);
           return AjaxResult.me().success();
       }catch (BussinessException e){
           e.printStackTrace();
           return AjaxResult.me().fail(e.getMessage());
       }
       catch (Exception e){
           e.printStackTrace();
           return AjaxResult.me().fail("系统繁忙，请稍后再试！！");
       }
    }
   /** 管理员审核通过 */
   @PostMapping("/audit/pass")
   public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog){
       try {
           shopService.auditPass(shopAuditLog);
       } catch (MessagingException e) {
           e.printStackTrace();
       }
       return AjaxResult.me().success();

   }
    /** 驳回 */
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        shopService.auditReject(shopAuditLog);
        return AjaxResult.me().success();
    }
    /**
     * 商家激活修改店铺状态
     */
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id) throws MessagingException {
        shopService.active(id);

        return AjaxResult.me().success();
    }
    /*   导出/export/excel*/
    @GetMapping("/export/excel")
    public void export(HttpServletResponse resp) {
        List<Shop> all = shopService.findAll();
        ExcelUtil.exportExcel(all,"","sheet1",Shop.class,"店铺列表.xlsx",resp);

    }
    /**
     * 导入
     */
    @PostMapping("/import/excel")
    @PreAuthorize(name = "商店的批量导入",sn = "shop:importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 2, Shop.class);
        shopService.importExcel(shops);
    }
    /**
     * /shop/eacher
     * 商店状态图形展示
     */
    @GetMapping("/eacher")
    //@PreAuthorize(name = "商店状态图形展示",sn = "shop:eacher")
    public List<ShopDto> eacher() {
        return shopService.eacher();

    }
}
